package com.zrrd.network.tcp.server;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * @author WanJl
 * @version 1.0
 * @title Server02
 * @description 服务器：先输入，再输出
 * @create 2023/10/22 15:26
 */
public class Server02 {
    public static void response(int port,String content) throws IOException {
        //1、创建服务器Socket对象 ServerSocket
        ServerSocket server = new ServerSocket(port);
        //2、监听连接的socket
        Socket socket = server.accept();
        //3、先输入再输出，获取并且处理输入流
        //再把字符输入流放到字符缓冲输入流中。
        BufferedReader br = new
                BufferedReader(new InputStreamReader(socket.getInputStream()));
        //5、使用while循环，不断的读取
        String line;
        while ((line=br.readLine())!=null){
            //操作接收到的数据
            System.out.println(line);
        }

        //6、获取输出流，向客户端反馈信息
        BufferedWriter bw =
                new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
        //写信息
        bw.write(content);
        //换行方法
        bw.newLine();
        //强行刷新，即使缓冲区不满，也必须提交。
        bw.flush();

        //7、释放资源
        bw.close();
        br.close();
        socket.close();
        server.close();

    }

    public static void main(String[] args) throws IOException {
        response(10010,"你TM来干啥？");
    }
}
